<template>
  <div class="exam" v-loading="loadingS">
    <Trans
      @imgChange="getImg"
      @changeImg="changeImg"
      :imgSrc="getItemPhoto(formPass.photo)"
      style="width: 30%; margin: auto"
    ></Trans>
    <el-form label-width="80px">
      <el-row :gutter="24">
        <el-col :span="8" style="padding: 0">
          <el-form-item label="姓名" prop="name">
            <el-input class="normal_input" v-model="formPass.name" style="width: 200px" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8" style="padding: 0">
          <el-form-item label="电话" prop="phone">
            <el-input class="normal_input" v-model="formPass.phone" style="width: 200px" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8" style="padding: 0">
          <el-form-item label="身份证号" prop="idCard">
            <el-input class="normal_input" v-model="formPass.idCard" style="width: 200px" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8" style="padding: 0">
          <el-form-item label="人员类型" prop="typeS">
            <el-select
              @change="changeType"
              class="normal_input"
              style="width: 200px"
              v-model="formPass.type"
              placeholder="请选择"
              clearable
            >
              <el-option
                v-for="item in houseList"
                :key="item.DID"
                :label="item.name"
                :value="item.DID"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8" style="padding: 0">
          <el-form-item label="学历">
            <el-select
              class="normal_input"
              style="width: 200px"
              v-model="form.educationType"
              placeholder="请选择"
              clearable
            >
              <el-option
                v-for="item in education"
                :key="item.DID"
                :label="item.name"
                :value="item.DID"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8" style="padding: 0">
          <el-form-item label="政治面貌">
            <el-select
              class="normal_input"
              style="width: 200px"
              v-model="formPass.political"
              placeholder="请选择"
              clearable
            >
              <el-option v-for="item in politics" :key="item.DID" :label="item.name" :value="item.DID"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8" style="padding: 0">
          <el-form-item label="选择小区" prop="CID">
            <el-select
              filterable
              @change="change_CID"
              style="width: 200px"
              class="normal_input"
              v-model="formPass.CID"
              placeholder="请选择"
              clearable
            >
              <el-option
                v-for="item in villageList"
                :key="item.CID"
                :label="item.name"
                :value="item.CID"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24" style="padding: 0">
          <el-form-item
            v-if="
              form.type !== '029f992a-d6e4-4421-baeb-88c9d2a15f3f' &&
              form.type !== '3a14543e-2808-431c-a0cf-98a64f41bd78'
            "
            required
            label="地址"
          >
            <div class="row" style="width: calc(100% - 20px)">
              <el-select
                prop="BID"
                class="normal_input"
                style="width: 100%"
                v-show="!isXiangCun"
                v-model="BID"
                @change="pickdong"
                placeholder
                clearable
              >
                <div>
                  <el-option
                    v-for="(item, idx) in maxdong"
                    :key="idx"
                    :label="item.name"
                    :value="item.BID"
                  ></el-option>
                </div>
              </el-select>
              <span v-show="!isXiangCun" style="width: 200px; text-align: center">栋</span>
              <el-select
                v-show="!isXiangCun"
                prop="UID"
                style="width: 100%"
                v-model="UID"
                @change="pickdanyuan"
                placeholder
                clearable
              >
                <el-option
                  v-for="(item, idx) in maxdanyuan"
                  :value="item.UID"
                  :label="item.name"
                  :key="idx"
                ></el-option>
              </el-select>
              <span v-show="!isXiangCun" style="width: 300px; text-align: center">单元</span>
              <el-select
                v-show="!isXiangCun"
                prop="FID"
                style="width: 100%"
                v-model="FID"
                @change="pickceng"
                placeholder
                clearable
              >
                <el-option
                  v-for="(item, idx) in maxceng"
                  :value="item.FID"
                  :label="item.name"
                  :key="idx"
                ></el-option>
              </el-select>
              <span v-show="!isXiangCun" style="width: 200px; text-align: center">层</span>
              <el-select prop="HID" style="width: 100%" v-model="HID" placeholder clearable>
                <el-option
                  v-for="(item, idx) in maxhao"
                  :value="item.HID"
                  :label="item.name"
                  :key="idx"
                ></el-option>
              </el-select>
              <span style="width: 200px; text-align: center">号</span>
            </div>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <div class="row" v-if="formPass.attached_photo">
      <span>材料证明:</span>
      <!--      <div>-->
      <!--        <img v-for="item in formPass.certificate" :key="item" :src="getItemPhoto(item)" v-image-preview alt-->
      <!--          style="width: 150px; height: 150px; padding: 10px" />-->
      <!--      </div>-->
      <div v-if="formPass.attached_photo" style="padding: 10px">
        <img :src="getItemPhoto(formPass.attached_photo)" v-image-preview alt style="width: 50px; height: 50px" />
      </div>
    </div>
    <el-form style="padding-right: 20px">
      <el-form-item label="审核说明：" prop="explain">
        <el-input type="textarea" v-model="describe" autocomplete="on" clearable></el-input>
      </el-form-item>
    </el-form>
    <deal-table ref="child" :form-data="formPass"></deal-table>
    <!-- <el-form style="padding-right: 20px">
      <el-form-item label="审核说明1：" prop="explain">
        <el-input type="textarea" v-model="formPass.describe" autocomplete="on" clearable></el-input>
      </el-form-item>
    </el-form> -->
    <el-button type="success" @click="handleSuccess">通过</el-button>
    <el-button type="danger" @click="handleFail">不通过</el-button>
  </div>
</template>

<script>
import Trans from '@/components/img/transform';
export default {
  components: {
    Trans
  },
  props: {
    /* 表单数据 */
    formPass: {
      type: Object,
      default: {}
    }
  },
  data() {
    return {
      describe: '',
      loadingS: false,
      form: {
        _id: '',
        CID: '', //小区ID
        PID: '', //人员ID
        photo: '', //要审核人的照片
        auditType: '', //是否通过
        auditPID: '', //审核人员
        idCard: '', //idcard
        name: '', //姓名
        type: '', //人员类型
        registration: '', //人员注册方式
        perID: '',
        perName: '',
        address: '',
        startTime: '',
        endTime: '',
        educationType: '',
        describe: '',
        attached_photo: '' //资料的照片
      },
      listVill: '',
      countries: this.$store.state.app.countries || [],
      nations: this.$store.state.app.nations,
      // education: [],
      houseList: [],
      // politics: [],
      // 小区列表
      villageList: [],
      perID: '',
      perName: '',
      maxdong: [],
      maxdanyuan: [],
      maxceng: [],
      maxhao: [],
      BID: '',
      UID: '',
      FID: '',
      HID: '',
      dong: '',
      danyuan: '',
      ceng: '',
      hao: '',
      education: [],
      politics: [],
      getCommunity: [],
      sysCertificateType: [],
      houser: { BID: '', UID: '', FID: '', HID: '' },
      isAbroad: false,
      sys_visa_type: {},
      isXiangCun: false
    };
  },

  methods: {
    judgeType(cid) {
      let coms = this.villageList.find((it) => it.CID === cid);
      if (coms && coms.type === '1cec27e8-8831-4e87-9180-061aeac3d9d1') return true; // 乡村房
      return false;
    },
    // 小区查询
    async getCommunityList() {
      this.villageList = await this.getCommunitiesMix();
      this.isXiangCun = this.judgeType(this.formPass.CID);
    },
    initHouse(e) {
      if (e === 'dongInt') this.maxdong = [];
      this.maxdanyuan = [];
      this.maxceng = [];
      this.maxhao = [];
      this.BID = '';
      this.UID = '';
      this.FID = '';
      this.HID = '';
    },
    // 根据选择的小区cid获取楼栋
    change_CID(e) {
      if (!e) {
        this.initHouse('dongInt');
        return;
      }
      let isXiangCun = this.judgeType(e);
      this.isXiangCun = isXiangCun;
      if (isXiangCun) {
        this.pickceng();
        return;
      }
      this.getMaxDong(e);
    },
    async getMaxDong(e) {
      let res = await this.$api.build.building_list_weChat({
        page: 1,
        limit: 1000,
        type: 1,
        sort: { name: -1 },
        where: { CID: e }
      });
      if (res.code !== 0) return;
      this.maxdong = res.data;
      this.initHouse();
    },
    //选择完楼栋
    async pickdong(e) {
      let res = await this.$api.build.building_list_weChat({
        page: 1,
        limit: 1000,
        type: 2,
        sort: { name: -1 },
        where: { BID: e, CID: this.formPass.CID }
      });
      if (res.code !== 0) return;
      this.maxdanyuan = res.data;
      this.maxceng = [];
      this.maxhao = [];
      this.UID = '';
      this.FID = '';
      this.HID = '';
    },
    async pickdanyuan(e) {
      let res = await this.$api.build.building_list_weChat({
        page: 1,
        limit: 1000,
        type: 3,
        sort: { name: -1 },
        where: { CID: this.formPass.CID, UID: e }
      });
      if (res.code !== 0) return;
      this.maxceng = res.data;
      this.maxhao = [];
      this.FID = '';
      this.HID = '';
    },
    async pickceng(e) {
      let res = await this.$api.build.building_list_weChat({
        page: 1,
        limit: 1000,
        type: 4,
        sort: { name: -1 },
        where: { CID: this.formPass.CID, FID: e }
      });
      if (res.code !== 0) return;
      this.maxhao = res.data;

      this.BID = res.data[0].BID;
      this.UID = res.data[0].UID;
      this.FID = res.data[0].FID;
      this.HID = '';
    },
    // 审核通过
    handleSuccess() {
      // 拷贝源数据
      let that = this;
      this.loadingS = true;
      this.formPass.describe = this.describe;
      let params = { ...this.formPass };
      params.houser = {
        BID: this.BID,
        UID: this.UID,
        FID: this.FID,
        HID: this.HID
      };
      params.CID = params.CID;
      this.$http
        .post(`${this.global.baseUrl}/admin/service/get_community_msg`, {
          CID: params.CID
        })
        .then((res) => {
          if (res.code == 0) {
            let address_t = '';
            if (res.data && res.data.address) {
              address_t = res.data.address;
            }
            if (params.houser.BID && params.houser.UID && params.houser.FID && params.houser.HID) {
              let aa = that.maxdong.find((v) => {
                return v.BID == params.houser.BID;
              });
              let bb = that.maxdanyuan.find((v) => {
                return v.UID == params.houser.UID;
              });
              let cc = that.maxceng.find((v) => {
                return v.FID == params.houser.FID;
              });
              let dd = that.maxhao.find((v) => {
                return v.HID == params.houser.HID;
              });

              params.address = `${address_t}${aa ? aa.name : ''}栋${bb ? bb.name : ''}单元${cc ? cc.name : ''}层${
                dd ? dd.name : ''
              }号`;
            } else {
              params.address = this.formPass.address;
            }
            this.$api.PlatformEntry.sys_person_edit(JSON.stringify(params)).then((res) => {
              if (res.code == 0) {
                let param = JSON.parse(JSON.stringify(this.formPass));
                param.auditType = 1;
                param.address = params.address;
                this.$api.personPass.personAudit(JSON.stringify(param)).then((res) => {
                  this.loadingS = false;
                  if (res.code == 0) {
                    this.dialogTableVisible = false;
                    this.$emit('getexam');
                    this.$message({
                      type: 'success',
                      message: '审核通过'
                    });
                  } else {
                    this.$message({
                      type: 'error',
                      message: res.msg
                    });
                    this.formPass.describe = res.msg;
                    this.describe = this.formPass.describe;
                  }
                });
              } else {
                this.loadingS = false;
                this.$message({
                  message: '操作失败,请联系管理员',
                  type: 'error'
                })
                  .catch((err) => {
                    this.loadingS = false;
                  })
                  .finally(() => {
                    this.loadingS = false;
                  });
              }
            });
          }
        });
    },
    //审核不通过
    handleFail() {
      this.formPass.auditType = 0;
      console.log(this.formPass);
      if (!this.formPass.describe) {
        this.$message.warning('不通过时需要填写审核说明！！！');
        return;
      }
      let param = JSON.stringify(this.formPass);
      // param.describe=this.describe
      this.loadingS = true;
      this.$api.personPass.personAudit(param).then((res) => {
        if (res.code == 0) {
          this.loadingS = false;
          this.$message({
            type: 'success',
            message: '审核不通过'
          });
          this.dialogTableVisible = false;
          this.$emit('getexam');
        } else {
          this.loadingS = false;
          this.$message({
            message: '操作失败, ' + res.msg,
            type: 'error'
          });
          this.$emit('getexam');
        }
      });
    },
    //人员类型改变
    changeType(e) {
      if (
        this.formPass.type === '029f992a-d6e4-4421-baeb-88c9d2a15f3f' ||
        this.formPass.type === '3a14543e-2808-431c-a0cf-98a64f41bd78'
      ) {
        this.BID = '';
        this.UID = '';
        this.FID = '';
        this.HID = '';
      }
    },
    changeImg(id) {
      this.formPass.photo = id;
    },
    // 图片旋转
    getImg(e) {
      // console.log(22)
      var paramd = {
        img: e
      };
      this.$api.imgAdd.addimgPer(paramd).then((res) => {
        if (res.code == 0) {
          this.formPass.photo = res.id;
          let paramJson = JSON.stringify(this.formPass);
          this.$http;
          this.$api.PlatformEntry.sys_person_edit(paramJson).then((res) => {
            if (res.code == 0) {
              this.$message({
                type: 'success',
                message: '图片修改成功'
              });
            } else {
              this.$message({
                type: 'success',
                message: '图片修改成功'
              });
            }
          });
        }
      });
    }
  },

  async mounted() {
    this.getCommunityList();
    let dictList = JSON.parse(sessionStorage.getItem('dictData'));
    this.education = dictList.sys_education_type;
    this.houseList = dictList.sys_person_type;
    this.politics = dictList.sys_politics_type;
    // 这里 获取到小区id  请求 小区下拉列表
    if (this.formPass.CID) {
      this.isXiangCun = this.judgeType(this.formPass.CID);
      await this.getMaxDong(this.formPass.CID);
      await this.pickdong(this.formPass.houser[0].BID);
      await this.pickdanyuan(this.formPass.houser[0].UID);
      await this.pickceng(this.formPass.houser[0].FID);
      this.BID = this.formPass.houser[0].BID;
      this.UID = this.formPass.houser[0].UID;
      this.FID = this.formPass.houser[0].FID;
      this.HID = this.formPass.houser[0].HID;
      this.formPass.old_cid = this.formPass.CID;
      this.formPass.old_idCard = this.formPass.idCard;
      this.formPass.old_auditType = this.formPass.auditType;
    }
  }
};
</script>
<style scoped>
.exam {
  width: 100%;
}
</style>
